import axios from 'axios'
import token from '@/utils/token'
import config from '@/config'
import store from '@/store'
import * as tools from './tools'
import qs from 'qs'

//  创建axios实例
const service = axios.create({
    //  请求超时时间
    timeout: 5000000,
    baseURL: config.apiContextPath
})

//  request拦截器
service.interceptors.request.use(
    cfg => {
        cfg.headers[token.getKey()] = token.get()
        return cfg
    },
    error => {
        return Promise.reject(error)
    }
)
//  创建axios实例
const uploadService = axios.create({
    //  请求超时时间
    timeout: 5000000,
    baseURL: config.apiContextPath
})
//  request拦截器
uploadService.interceptors.request.use(
    cfg => {
        cfg.headers["Content-Type"] = "multipart/form-data"
        return cfg
    },
    error => {
        return Promise.reject(error)
    }
)
//  response 拦截器
service.interceptors.response.use(
    res => {
        if (res.status === 200) {
            const resp = res.data
            if (resp.code === 200) {
                return Promise.resolve(resp)
            } else if (resp.code === 204) {
                //  token超时，重新登录
                tools.notify({
                    type: 'error',
                    message: resp.msg
                })
                store.dispatch('user/logoutHandler').then(() => {
                    window.sessionStorage.clear()
                    window.location.reload() //  为了重新实例化vue-router对象 避免bug
                })
                //  return Promise.reject(resp.msg)
            } else {
                tools.notify({
                    type: 'error',
                    message: resp.msg
                })
                return Promise.reject(resp.msg)
            }
        }
    },
    error => {
        if (error.response && error.response.status === 401) {
            store.dispatch('user/logoutHandler').then(() => {
                window.sessionStorage.clear()
                window.location.reload() //  为了重新实例化vue-router对象 避免bug
            })
        }
        return Promise.reject(error)
    }
)

const post = (url, paramObj = {}, stringify = false) => {
    return new Promise((resolve, reject) => {
        service
            .post(url, stringify ? qs.stringify(paramObj, {indices: false}) : paramObj)
            .then(resp => resolve(resp))
            .catch(err => reject(err))
    })
}
const uploadPost = (url, paramObj) => {
    return new Promise((resolve, reject) => {
        uploadService
            .post(url, paramObj)
            .then(resp => resolve(resp))
            .catch(err => reject(err))
    })
}
const get = (url, paramObj = {}) => {
    return new Promise((resolve, reject) => {
        service
            .get(url, {
                params: paramObj
            })
            .then(resp => resolve(resp))
            .catch(err => reject(err))
    })
}

const download = (url, paramObj = {}) => {
    return new Promise((resolve, reject) => {
        service
            .get(url, paramObj, {
                responseType: 'blob'
            })
            .then(resp => resolve(resp))
            .catch(err => reject(err))
    })
}

export default {
    post,
    get,
    download,
    uploadPost,
}
